<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Catalog extends CI_Controller {
	public $page_name = "catalog";
	public $v_config = array(
	    array(
	        'field' => 'name',
	        'label' => '名称',
	        'rules' => 'required'
	    )
	);
   public function __construct(){
    parent::__construct();
    if(!$this->session->userdata('is_login')){
            redirect('/admin/auth/login');
        }
    }
	public function index(){
        $this->load->model('Catalog_model');
        $data=array(
            'catalogs'=>$this->Catalog_model->getall(),
        );
        $this->load->view('admin/Catalog_index',$data);
	}
    public function add(){
        $this->load->library('form_validation');
        $this->form_validation->set_rules($this->v_config);
        $this->load->model('Catalog_model');
        $this->Catalog_model->getall();
        $catalog = new stdClass();
        $catalog->name="";
        $data = array(
                'title'=>"添加",
                'catalog'=>$catalog
                );
        if ($this->form_validation->run() == FALSE){
                $this->load->view('/admin/catalog_form',$data);
            }else{ 
                $data = $this->input->post();
                $this->load->model('Catalog_model');
                $this->Catalog_model->add_one($data);
                redirect('/admin/catalog/index',$data);
        }
    }
    public function eidt($id){
        $this->load->library('form_validation');
        $this->form_validation->set_rules($this->v_config);
        $this->load->model('Catalog_model');
        if ($this->form_validation->run() == FALSE){
            $data = array(
                'title'=>"编辑",
                'catalog'=>$this->Catalog_model->get_one($id),
            );
                $this->load->view('/admin/catalog_form',$data);
            }else{ 
                $data = $this->input->post();
                $this->Catalog_model->edit_one($id,$data);
                redirect('/admin/catalog/index');
        }
    }
    public function del($id){
        $this->load->model('Catalog_model');
        $data = array(
            'catalog'=>$this->Catalog_model->del($id)
            );
        redirect('/admin/catalog/index',$data );
    }

}
